// @ts-nocheck
import {reactive, ref} from "vue";
import {ElMessage} from "element-plus";
import {axios_request} from "@/api/milliaAxios";
import {registerForm} from "@/static/Login_Register/register";
import {editForm} from "@/static/Homepage/Personal_data";
import {forgetForm} from "@/static/Login_Register/Reset_pwd";

const sendCodeForm = reactive({
    telephone: '',
})

// 验证码按钮功能
export const messageCodeVis = ref(false)
export const countdown = ref(0);

export const sendCode = async (key: string) => {
    const reg = /^1[3-9]\d{9}$/;
    if (key == "register"){
        sendCodeForm.telephone = registerForm.telephone
    } else if (key == "reset_telephone"){
        sendCodeForm.telephone = editForm.telephone
    } else if (key == "reset_pwd"){
        sendCodeForm.telephone = forgetForm.telephone
        console.log(sendCodeForm.telephone)
    }

    if (!sendCodeForm.telephone) {
        return ElMessage.warning('请输入手机号码')
    }

    if (reg.test(sendCodeForm.telephone)) {
        console.log("发送验证码... ...")

        //发送短信的api接口
        const res = await axios_request('/api/user/register/send_sms_code/', JSON.stringify(sendCodeForm), 'post');
        console.log("POST请求返回的数据：" + res);
        console.log(res);

        if (res.status == 200) {
            messageCodeVis.value = true
            countdown.value = 60
            startCountdown.start()
            ElMessage.success(res.data.msg)
        }else {
            ElMessage.warning(res.data.msg)
        }
    } else {
        ElMessage.warning('请输入正确的手机号码')
    }
}

// 倒计时
export const startCountdown = reactive({
    intervalID: null,
    start: () => {
        startCountdown.intervalID = setInterval(() => {
            countdown.value--;
            if (countdown.value <= 0) {
                clearInterval(startCountdown.intervalID);
                messageCodeVis.value = false;
                countdown.value = 0;
            }
        }, 1000);
    },
});